package com.campus.controller;

import com.campus.dto.TeacherDto;
import com.campus.model.Teacher;
import com.campus.service.TeacherService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * @desc:
 * @author: Tournesol234
 * @date: 2024/11/22 14.41
 */
@RestController
@RequestMapping("/teachers")
public class TeacherController {
    private final TeacherService teacherService;

    public TeacherController(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    @PostMapping("/create")
    public ResponseEntity<String> createTeacher(@RequestBody TeacherDto teacherDto) {
        int result = teacherService.createTeacher(teacherDto);
        return result > 0 ? ResponseEntity.ok("Teacher created successfully") : ResponseEntity.status(500).body("Failed to create teacher");
    }

    @GetMapping("/{id}")
    public ResponseEntity<Teacher> getTeacherById(@PathVariable int id) {
        Teacher teacher = teacherService.getTeacherById(id);
        return teacher != null ? ResponseEntity.ok(teacher) : ResponseEntity.notFound().build();
    }

    @GetMapping("/filter")
    public ResponseEntity<List<Teacher>> getTeachersByFilter(@RequestParam(required = false) String subject,
                                                             @RequestParam(required = false) String school) {
        List<Teacher> teachers = teacherService.getTeachersByFilter(subject, school);
        return ResponseEntity.ok(teachers);
    }

    @PutMapping("/{id}/update")
    public ResponseEntity<String> updateTeacher(@PathVariable int id, @RequestBody TeacherDto teacherDto) {
        teacherDto.setId(id);
        int result = teacherService.updateTeacher(teacherDto);
        return result > 0 ? ResponseEntity.ok("Teacher updated successfully") : ResponseEntity.status(500).body("Failed to update teacher");
    }

    @DeleteMapping("/{id}/delete")
    public ResponseEntity<String> deleteTeacher(@PathVariable int id) {
        int result = teacherService.deleteTeacher(id);
        return result > 0 ? ResponseEntity.ok("Teacher deleted successfully") : ResponseEntity.status(500).body("Failed to delete teacher");
    }
}
